package com.smarttiger.zero.admin.task;

import com.smarttiger.zero.admin.dao.TaskLogicDao;
import com.smarttiger.zero.admin.entity.LogicModel;
import com.smarttiger.zero.admin.util.SpringContextUtils;
import com.smarttiger.zero.logic.ExecuteLogics;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.List;

/**
 * Created by zhouzhengwei on 2021/11/13.
 */
public class SchedulerQuartzJob implements Job{
    private void before(String taskName){
        System.out.println(taskName+"任务开始");
    }

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {

        String taskId = arg0.getJobDetail().getJobDataMap().getString("taskId");
        String taskName = arg0.getJobDetail().getJobDataMap().getString("taskName");
        before(taskName);
        TaskLogicDao taskLogicDao = (TaskLogicDao) SpringContextUtils.getBean("taskLogicDao");
        List<LogicModel> logics = taskLogicDao.getLogicByTaskId(taskId);
        try {
            ExecuteLogics.executeLogics(logics);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        after(taskName);
    }

    private void after(String taskName){
        System.out.println(taskName+"任务结束");
    }

}

